package com.tian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tian.entity.UserAdmin;
import com.tian.mapper.RoleMapper;
import com.tian.mapper.UserAdminMapper;
import com.tian.service.UserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tian
 */
@Service
@Transactional
public class UserAdminServiceImpl extends ServiceImpl<UserAdminMapper, UserAdmin> implements UserAdminService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public boolean save(UserAdmin entity) {
        return super.save(entity);
    }

    @Override
    public boolean updateById(UserAdmin entity) {
        return super.updateById(entity);
    }

    @Override
    public UserAdmin getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean removeById(Serializable id) {
        //根据用户ID删除用户角色中间表的数据
        roleMapper.deleteRoleUserByUid(id);
        return super.removeById(id);
    }

    @Override
    public void saveUserRole(Integer uid, Integer[] ids) {
        //根据用户ID删除sys_role_user里面的数据
        this.roleMapper.deleteRoleUserByUid(uid);
        if (null != ids && ids.length > 0) {
            for (Integer rid : ids) {
                this.roleMapper.insertUserRole(uid, rid);
            }
        }
    }
}
